﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TekaMUD.Engine.Blocks;
using TekaMUD.Engine.IO;
using TekaMUD.Engine.Behaviour;

namespace TekaMUD.Engine.Commands
{
	public class InventoryActions
	{
		public static void Show_Inventory(Command c)
		{
			GameObject[] contents = c.Player.Backpack.Contents;

			if (contents.Length == 0)
			{
				Messenger.SendAgent(c.Player, "You are carrying nothing in your backpack.");
				return;
			}


			StringBuilder sb = new StringBuilder("You are carrying:\n\n");

			foreach (GameObject go in c.Player.Backpack.Contents)
			{
				if (go is IUncountable)
				{
					IUncountable gou = go as IUncountable;

					sb.Append("\t" + gou.Quantity + " " + gou.Unit + " of " + go.Name + "\n");
				}
				else
				{
					sb.Append("\t" + go.Article + " " + go.AdjectivizedName + "\n");
				}
			}

			sb.Append("\n");
			Messenger.SendAgent(c.Player, sb.ToString());
		}

		/// <summary>
		/// Drop Action
		/// </summary>
		public static void Drop(Command c)
		{
			string SelfExecutionMsg = "You drop {0} on the ground.";
			string OtherExecutionMsg = "{0} drops {1} on the ground.";

			List<GameObject> added = new List<GameObject>();

			if (c.BoundArguments == null || c.BoundArguments.Length == 0)
			{
				Messenger.SendAgent(c.Player, "Drop what?\r\n");
				return;
			}

			foreach (GameEntity ge in c.BoundArguments)
			{
				if (!(ge is GameObject))
				{
					Messenger.SendAgent(c.Player, "You can't drop that.\r\n");
				}

				GameObject go = (GameObject) ge;

				if (c.Player.Backpack.TryAndRemove(go))
				{
					if (!c.Player.CurrentRoom.RoomObjects.Store(go))
					{
						// Return the object to the player.
						c.Player.Backpack.Store(go);
						break;
					}
					else
					{
						added.Add(go);
					}
				}
			}

			if (added.Count > 0)
			{
				StringBuilder sb = new StringBuilder();
				bool first = true;
				int counter = 1;

				foreach (GameObject ago in added)
				{
					if (first)
						first = false;
					else
						if (added.Count > 1 && counter == added.Count)
							sb.Append(" and ");
						else
							sb.Append(", ");

					sb.Append(ago.Article);
					sb.Append(" ");
					sb.Append(ago.AdjectivizedName);

					counter++;
				}

				Messenger.SendRoom(c.Player.CurrentRoom, string.Format(OtherExecutionMsg, c.Player.Name, sb.ToString()), c.Player);
				Messenger.SendAgent(c.Player, string.Format(SelfExecutionMsg, sb.ToString()));
			}
		}

		/// <summary>
		/// Pick Action
		/// </summary>
		public static void Pick(Command c)
		{
			string SelfExecutionMsg = "You pick {0} from the ground.";
			string OtherExecutionMsg = "{0} picks {1} from the ground.";
			List<GameObject> added = new List<GameObject>();

			if (c.BoundArguments == null || c.BoundArguments.Length == 0)
			{
				Messenger.SendAgent(c.Player, "Pick what?\r\n");
				return;
			}

			foreach (GameEntity ge in c.BoundArguments)
			{
				if(!(ge is IPickable))
				{
					 Messenger.SendAgent(c.Player, "You can't pick that.\r\n");
					 continue;
				}

				GameObject go = (GameObject)ge;

				if (c.Player.Backpack.Quantity >= c.Player.Backpack.MaximumItems)
				{
					Messenger.SendAgent(c.Player, "You cannot hold any more objects!\r\n");
					break;
				}

				if (c.Player.CurrentRoom.RoomObjects.TryAndRemove(go))
				{
					if (!c.Player.Backpack.Store(go))
					{
						// Return the object to the ground.
						c.Player.CurrentRoom.RoomObjects.Store(go);
						break;
					}
					else
					{
						added.Add(go);
					}
				}
			}

			if (added.Count > 0)
			{
				StringBuilder sb = new StringBuilder();
				bool first = true;
				int counter = 1;

				foreach (GameObject ago in added)
				{
					if (first)
						first = false;
					else
						if (added.Count > 1 && counter == added.Count)
							sb.Append(" and ");
						else
							sb.Append(", ");

					sb.Append(ago.Article);
					sb.Append(" ");
					sb.Append(ago.AdjectivizedName);

					counter++;
				}

				Messenger.SendRoom(c.Player.CurrentRoom, string.Format(OtherExecutionMsg, c.Player.Name, sb.ToString()), c.Player);
				Messenger.SendAgent(c.Player, string.Format(SelfExecutionMsg, sb.ToString()));
			}
			else
			{
				Messenger.SendAgent(c.Player, "Nothing to pick.\r\n");
			}
		}
	}
}
